﻿using System;
using System.Linq;
using MarriageFertility.Helpers;
using MarriageFertility.Mcm;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.CharacterDevelopment;
using TaleWorlds.CampaignSystem.ComponentInterfaces;

namespace MarriageFertility.Models
{
    public class MfPregnancyModel : PregnancyModel
    {
        private readonly PregnancyModel _previousModel;
        public MfPregnancyModel(PregnancyModel previousModel)
        {
            _previousModel = previousModel;
        }
        private int MinPregnancyAge => MCMSettings.Instance.MinPregnancyAge;
        private int MaxPregnancyAge => MCMSettings.Instance.MaxPregnancyAge;

        public override float PregnancyDurationInDays => MCMSettings.Instance.PregnancyDurationInDays;

        public override float MaternalMortalityProbabilityInLabor => MCMSettings.Instance.MaternalMortalityProbabilityInLabor / 100;

        public override float StillbirthProbability => MCMSettings.Instance.StillbirthProbability / 100;

        public override float DeliveringFemaleOffspringProbability => MCMSettings.Instance.DeliveringFemaleOffspringProbability / 100;

        public override float DeliveringTwinsProbability => MCMSettings.Instance.DeliveringTwinsProbability / 100;

        private bool IsHeroAgeSuitableForPregnancy(Hero hero) => hero.Age >= MinPregnancyAge && hero.Age <= MaxPregnancyAge;
        private int GetClanChildCount(Clan clan)
        { 
            if (clan?.Heroes == null) return 0;
            return clan.Heroes.Count(h => h.IsAlive && h.IsChild);
        }
        
        // 获取英雄每天怀孕的概率。
        public override float GetDailyChanceOfPregnancyForHero(Hero hero)
        {
            if (hero == null || !hero.IsAlive || !hero.IsFemale || hero.IsPregnant)
                return 0f;

            var mcmSettings = MCMSettings.Instance; // 提前提取，减少重复访问
            var heroClan = hero.Clan;
            // 检查孩子数量是否超过上限
            if ((heroClan == Clan.PlayerClan && GetClanChildCount(Clan.PlayerClan) >= mcmSettings.PlayerMaxChildrenLimit) ||
                (heroClan != Clan.PlayerClan && GetClanChildCount(heroClan) >= mcmSettings.LordMaxChildrenLimit))
                return 0f;

            // 如果启用了怀孕概率修改，则直接返回相应的概率
            if (mcmSettings.EnablePregnancyModify)
            {
                if (hero.IsPlayer() || hero.IsPlayerSpouse()) 
                    return mcmSettings.MainHeroProbability / 100;
                if (hero.IsPlayerClan()) 
                    return mcmSettings.MainHeroClanProbability / 100;
                return mcmSettings.OtherHeroProbability / 100;
            }
            
            if (heroClan == null) return 0.1f;
            
            // 计算怀孕概率
            int numberOfChildren = hero.Children?.Count + 1 ?? 1; 
            float clanTierFactor = 4 + 4 * heroClan.Tier;
            float mainHeroFactor = hero == Hero.MainHero || hero.Spouse == Hero.MainHero 
                ? 1f 
                : Math.Min(1f, (2f * clanTierFactor - heroClan.Lords.Count) / clanTierFactor);

            float basePregnancyChance = (float)((1.2 - ((double)hero.Age - MinPregnancyAge) * 0.04) / (numberOfChildren * numberOfChildren) * 0.12) * mainHeroFactor;

            // 如果英雄或配偶年龄不适合怀孕，则返回0
            if (hero.Spouse == null || !IsHeroAgeSuitableForPregnancy(hero)) 
                return 0f;

            float finalPregnancyChance = basePregnancyChance;

            // 如果英雄或其配偶有“Virile”特质，增加怀孕概率
            if (hero.GetPerkValue(DefaultPerks.Charm.Virile) || (hero.Spouse != null && hero.Spouse.GetPerkValue(DefaultPerks.Charm.Virile)))
            {
                finalPregnancyChance *= 1 + DefaultPerks.Charm.Virile.PrimaryBonus;
            }

            return finalPregnancyChance;
        }


    }
}